package cn.com.homeyoda.aimate.aihealth.ui.my.family.recommend;

import android.os.Bundle;

import androidx.core.content.ContextCompat;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.wzq.mvvmsmart.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import cn.com.homeyoda.aimate.aihealth.BR;
import cn.com.homeyoda.aimate.aihealth.R;
import cn.com.homeyoda.aimate.aihealth.app.AppViewModelFactory;
import cn.com.homeyoda.aimate.aihealth.base.BaseActivity;
import cn.com.homeyoda.aimate.aihealth.bean.MemberBean;
import cn.com.homeyoda.aimate.aihealth.databinding.ActivityRecommendBinding;

public class RecommendActivity extends BaseActivity<ActivityRecommendBinding, RecommendViewModel> {

    private RecommendAdapter recommendAdapter;
    private List<MemberBean> membersList = new ArrayList<>();

    @Override
    public RecommendViewModel initViewModel() {
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getApplication());
        return ViewModelProviders.of(this, factory).get(RecommendViewModel.class);
    }

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_recommend;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initData() {
        super.initData();
        binding.titleBar.ivBack.setOnClickListener(v -> finish());
        binding.titleBar.tvTitle.setText(getString(R.string.family_management));
        binding.tvDeviceId.setText("本设备号：ABC123123123");

        viewModel.getUserData();

        initRecyclerView();
    }

    private void initRecyclerView() {

        recommendAdapter = new RecommendAdapter(R.layout.recommend_member_item, membersList);
        binding.setLayoutManager(new LinearLayoutManager(this));
        DividerItemDecoration divider = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
        divider.setDrawable(ContextCompat.getDrawable(this, R.drawable.divider_transparent_28));
        binding.rvRecommendMembers.addItemDecoration(divider);
        binding.setAdapter(recommendAdapter);
        recommendAdapter.setOnItemChildClickListener((adapter, view, position) ->
                ToastUtils.showShort(getString(R.string.send_invitation)));

    }


    @Override
    public void initViewObservable() {
        super.initViewObservable();
        viewModel.memberData.observe(this, memberBeans -> {
            membersList = memberBeans;
            recommendAdapter.setNewData(membersList);
        });
    }

}